package com.imay.live.activity.family.helper.session.viewholder;

import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.base.util.StringUtil;
import com.imay.live.R;
import com.imay.live.activity.RoomLiveNormalActivity;
import com.imay.live.activity.RoomLiveShortActivity;
import com.imay.live.activity.RoomPushNormalActivity;
import com.imay.live.activity.discovery.DynamicActivity;
import com.imay.live.activity.family.helper.session.extension.DynamicsAttachment;
import com.imay.live.activity.family.helper.session.extension.LiveAttachment;
import com.imay.live.activity.family.helper.session.extension.ShortLiveAttachment;
import com.imay.live.helper.UrlHelper;
import com.imay.live.manager.AccountManager;
import com.imay.live.utils.GlideUtil;
import com.imay.base.util.SpannableStringUtils;
import com.imay.live.utils.ViewUtil;
import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.netease.nim.uikit.session.viewholder.MsgViewHolderBase;

/**
 * Created by Administrator on 2017/9/14.
 */

public class MsgViewHolderDynamic extends MsgViewHolderBase {

    private ImageView iv_custom_msg_img,iv_custom_msg_img_right;
    private ImageView iv_custom_msg_type,iv_custom_msg_type_right;
    private TextView tv_custom_msg_content;
    private RelativeLayout rl_img_msg_custom,rl_img_msg_custom_right;
    private View tv_click_check_left,tv_click_check_right;

    public MsgViewHolderDynamic(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int rightBackground() {
        return R.drawable.bf_wshuru;
    }

    @Override
    protected int getContentResId() {
        return R.layout.item_msg_custom;

    }


    @Override
    protected void inflateContentView() {
        iv_custom_msg_img  = ViewUtil.findView(view,R.id.iv_custom_msg_img);
        iv_custom_msg_type  = ViewUtil.findView(view,R.id.iv_custom_msg_type);
        tv_custom_msg_content  = ViewUtil.findView(view,R.id.tv_custom_msg_content);
        rl_img_msg_custom  = ViewUtil.findView(view,R.id.rl_img_msg_custom);
        rl_img_msg_custom  = ViewUtil.findView(view,R.id.rl_img_msg_custom);
        tv_click_check_right  = ViewUtil.findView(view,R.id.tv_click_check_right);
        tv_click_check_left  = ViewUtil.findView(view,R.id.tv_click_check_left);
        iv_custom_msg_img_right  = ViewUtil.findView(view,R.id.iv_custom_msg_img_right);
        iv_custom_msg_type_right  = ViewUtil.findView(view,R.id.iv_custom_msg_type_right);
        rl_img_msg_custom_right  = ViewUtil.findView(view,R.id.rl_img_msg_custom_right);

    }

    @Override
    protected void bindContentView() {
        ImageView draweeView;
        ImageView typeImagView;
        boolean isReceive = isReceivedMessage();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) contentContainer.getLayoutParams();
        if (isReceive){
            draweeView = iv_custom_msg_img;
            typeImagView = iv_custom_msg_type;
            params.setMargins(0,0,(int)com.imay.base.util.ViewUtil.dip2px(context,32),0);
            contentContainer.setLayoutParams(params);
        }else{
            draweeView = iv_custom_msg_img_right;
            typeImagView = iv_custom_msg_type_right;
            params.setMargins((int)com.imay.base.util.ViewUtil.dip2px(context,32),0,0,0);
            contentContainer.setLayoutParams(params);

        }
        rl_img_msg_custom.setVisibility(isReceive?View.VISIBLE:View.GONE);
        tv_click_check_right.setVisibility(isReceive?View.VISIBLE:View.GONE);
        rl_img_msg_custom_right.setVisibility(isReceive?View.GONE:View.VISIBLE);
        tv_click_check_left.setVisibility(isReceive?View.GONE:View.VISIBLE);

        bindAttachmentData(draweeView,typeImagView);

    }

    private void bindAttachmentData(ImageView draweeView,ImageView typeImagView){
        if (message.getAttachment() instanceof  DynamicsAttachment){
            DynamicsAttachment attachment = (DynamicsAttachment) message.getAttachment();
            GlideUtil.loadImage(draweeView,attachment.getUrl());
            typeImagView.setBackgroundResource(attachment.isImage()?0:R.mipmap.ico_dongtai);
            String dynamic = StringUtil.isEmpty(attachment.getMsg())?context.getString(R.string.release_dynamic_without_content):context.getString(R.string.release_dynamic);
            SpannableStringBuilder builder = SpannableStringUtils.getBuilder(dynamic).setForegroundColor(context.getResources().getColor(R.color.text_70)).
                    append(attachment.getMsg()).setForegroundColor(context.getResources().getColor(R.color.text_0e)).create();
            tv_custom_msg_content.setText(builder);
        }else if (message.getAttachment() instanceof LiveAttachment){
            LiveAttachment attachment = (LiveAttachment) message.getAttachment();
            GlideUtil.loadImage(draweeView,attachment.getImgFace());
            typeImagView.setBackgroundResource(R.mipmap.ico_zhibo);
            String live = StringUtil.isEmpty(attachment.getTitle())?context.getString(R.string.liveing_without_content):context.getString(R.string.liveing);
            SpannableStringBuilder builder = SpannableStringUtils.getBuilder(live).setForegroundColor(context.getResources().getColor(R.color.text_70)).
                    append(attachment.getTitle()).setForegroundColor(context.getResources().getColor(R.color.text_0e)).create();
            tv_custom_msg_content.setText(builder);

        }else if (message.getAttachment() instanceof ShortLiveAttachment){
            ShortLiveAttachment attachment = (ShortLiveAttachment) message.getAttachment();
            GlideUtil.loadImage(draweeView,UrlHelper.getVideoThumbUrl(attachment.getVideoUrl()));
            typeImagView.setBackgroundResource(R.mipmap.ico_cdb);
            String shortLive = StringUtil.isEmpty(attachment.getTitle())?context.getString(R.string.release_short_live_without_content):context.getString(R.string.release_short_live);
            SpannableStringBuilder builder = SpannableStringUtils.getBuilder(shortLive).setForegroundColor(context.getResources().getColor(R.color.text_70)).
                    append(attachment.getTitle()).setForegroundColor(context.getResources().getColor(R.color.text_0e)).create();
            tv_custom_msg_content.setText(builder);
        }
    }

    @Override
    protected void onItemClick() {
        super.onItemClick();
        if (message.getAttachment() instanceof  DynamicsAttachment){
            DynamicsAttachment attachment = (DynamicsAttachment) message.getAttachment();
            DynamicActivity.start(context,attachment.getFeedId(),context.getString(R.string.str_imay_photo));
        }else if (message.getAttachment() instanceof LiveAttachment){
            LiveAttachment attachment = (LiveAttachment) message.getAttachment();
            if (attachment.getUid()== AccountManager.getInstance().getUid()) {
                RoomPushNormalActivity.startActivity(context);
            }else{
                RoomLiveNormalActivity.startActivity(context, attachment.getRoom_id(), attachment.getNick(), "", attachment.getUrl(), attachment.getImgFace(), attachment.getOpenLiveType(), attachment.getHorizontal());
            }
        }else if (message.getAttachment() instanceof ShortLiveAttachment){
            ShortLiveAttachment attachment = (ShortLiveAttachment) message.getAttachment();
            RoomLiveShortActivity.startLiveActivity(context,attachment.getShortLiveId(),true);

        }
    }
}
